
#coding:utf-8
import os
import re

import pandas as pd
import requests
from bs4 import BeautifulSoup


# curl http://10.11.255.23:5000/todo1 -d 'data_file=/home/yzfu/nlp/kg_abc/kg_nlp/ner/data/58afafc0cafd5b39afe6dc28.txt' -X put

class Extract_Index:

    #初始化
    def __init__(self,articles):
        self.articles = articles
        self.ner_url='http://10.11.255.23:5000/get/ner_result/'

    # load文章
    def load_article(self):
        # 计算机行业
        # 1、先抽取一整块内容
        soup = BeautifulSoup(self.articles, 'lxml')
        # 删除outline
        if hasattr(soup, 'div') and soup.div is not None:
            soup.div['class'] = 'outline'
            soup.div.extract()
        # 获取原始文档，剔除不需要的内容
        origin_text = soup.get_text()
        rule = r'核心竞争力分析(.*)第五节'
        if len(re.findall(rule, origin_text, re.S)) > 0:
            origin_text = re.findall(rule, origin_text, re.S)[0]
        data = pd.read_table('d:/gitee/abc_kg_nlp/data/dict/index_dict')
        key_rule = r'([\u4e00-\u9fa5]+)\d'
        value_rule = r'(\d+[元,万元,亿]{1})'
        compare_rule = r'([\u4e00-\u9fa5]+)\d'
        compare_value = r'\d+\%'
        for index in data['名称']:
            # 指标抽取
            if ':' in str(index):
                continue
            # 1.1 营业收入，比去年同期增长1415%
            #指标所处原文
            income_origin_rule =r'(.{1,90}'+index+'\d+\.?\d+[元,万元,亿]{1}.{1,12}同期增长\d+\.?\d+%{1})'
            income_origin_text = re.findall(income_origin_rule,origin_text,re.S)
            # if len(income_origin_text) > 0:
            #     print(income_origin_text)
            #指标抽取正则
            income_rule = r'('+index+'\d+\.?\d+[元,万元,亿]{1}.{1,12}同期增长\d+\.?\d+%{1})'
            income_text = re.findall(income_rule, origin_text, re.S)
            if len(income_text)>0:
                # print(income_text)
                #拆分为键值对  split ，
                arr_text = income_text[0].split('，')
                # print(re.findall(key_rule,arr_text[0]))
                # print(re.findall(value_rule,arr_text[0]))
                # print(re.findall(compare_rule,arr_text[1]))
                # print(re.findall(compare_value,arr_text[1]))
                # 指标通过正则取出后，的一个归属问题  ==> 查找最近的实体并做归属
                income_origin_text = income_origin_text[0].replace(income_text[0],'')
                print(income_origin_text)
                data = {"data_string":income_origin_text}
                data = requests.post(self.ner_url, data=data).json()
                # print(data)
                return data



if __name__=="__main__":
    dirpath = os.path.dirname(os.path.abspath('../data/result/text_01/origin_04.txt'))
    orgin_text = dirpath+"\origin_04.txt"
    file = open(orgin_text, 'r', encoding='UTF-8')
    lines = file.read()
    extract_util = Extract_Index(lines)
    extract_util.load_article()
